package com.volvo.smm.email;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class SendmailUtil {
     
    // 设置服务器
    private static String KEY_SMTP = "mail.smtp.host";
    private static String VALUE_SMTP = "smtp.163.com";
    // 服务器验证
    private static String KEY_PROPS = "mail.smtp.auth";
    private static boolean VALUE_PROPS = true;
    // 发件人用户名、密码
    private String SEND_USER = "volvosmm@163.com";
    private String SEND_UNAME = "volvosmm@163.com";
    private String SEND_PWD = "AAbb2016";
    // 建立会话
    private MimeMessage message;
    private Session s;
 
    /*
     * 初始化方法
     */
    public SendmailUtil() {
        Properties props = System.getProperties();
        props.setProperty(KEY_SMTP, VALUE_SMTP);
        props.put(KEY_PROPS, "true");
        //props.put("mail.smtp.auth", "true");
        s =  Session.getDefaultInstance(props, new Authenticator(){
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(SEND_UNAME, SEND_PWD);
              }});
        s.setDebug(true);
        message = new MimeMessage(s);
    }
 
    /**
     * 发送邮件
     * 
     * @param headName
     *            邮件头文件名
     * @param sendHtml
     *            邮件内容
     * @param receiveUser
     *            收件人地址
     */
    public void doSendHtmlEmail(String headName, String sendHtml,
            String receiveUser) {
        try {
            // 发件人
            InternetAddress from = new InternetAddress(SEND_USER);
            message.setFrom(from);
            // 收件人
            InternetAddress to = new InternetAddress(receiveUser);
            message.setRecipient(Message.RecipientType.TO, to);
            // 邮件标题
            message.setSubject(headName);
            String content = sendHtml.toString();
            // 邮件内容,也可以使纯文本"text/plain"
            message.setContent(content, "text/html;charset=GBK");
            message.saveChanges();
            Transport transport = s.getTransport("smtp");
            // smtp验证，就是你用来发邮件的邮箱用户名密码
            transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
            // 发送
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            System.out.println("send success!");
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
           
    	Date date = new Date();		
        SimpleDateFormat dateFormater  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now_time_str="";
        now_time_str = dateFormater.format(date);
    	
        String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String dbURL="jdbc:sqlserver://vrgbbqmya2.database.chinacloudapi.cn:1433;DatabaseName=smmReportcloud";
        String userName="WendleXu@vrgbbqmya2";
        String userPwd="Namlj3@808344";
        
        String mail_content="";
        
        Statement stmt=null;
        ResultSet rs=null;
        Connection dbConn=null;
        try{
	         Class.forName(driverName);
	         dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
	         System.out.println("连接数据库成功");
	         
	         stmt = dbConn.createStatement();//创建sql执行对象
	         
	         //当月批售总数
	         rs = stmt.executeQuery("sp_get_email_content");//执行sql语句并返回结果集
	         rs.next();
	         mail_content = rs.getString(1);
	         
	         
        }catch(Exception e){
	         e.printStackTrace();
	         System.out.print("连接失败");
        }finally{
			if(rs != null){   // 关闭记录集   
				try{   
				    rs.close() ;   
				}catch(SQLException e){   
				    e.printStackTrace() ;   
				}   
			}   

			if(stmt != null){   // 关闭声明   
				try{   
				    stmt.close() ;   
				}catch(SQLException e){   
				    e.printStackTrace() ;   
				}   
			}   
			
			if(dbConn != null){  // 关闭连接对象   
				try{   
					 dbConn.close() ;   
				}catch(SQLException e){   
				    e.printStackTrace() ;   
				}   
			}  
        }
        
        SendmailUtil se = new SendmailUtil();
        
        //se.doSendHtmlEmail("沃尔沃销售每日快讯", mail_content, "jinzhu.jin@volvocars.com");
        //se.doSendHtmlEmail("沃尔沃销售每日快讯", mail_content, "matthew.xie@volvocars.com");
        //se.doSendHtmlEmail("沃尔沃销售每日快讯", mail_content, "dongping.xiang@volvocars.com");
        //se.doSendHtmlEmail("沃尔沃销售每日快讯", mail_content, "peiji.qin@volvocars.com");
        //se.doSendHtmlEmail("沃尔沃销售每日快讯", mail_content, "cailin.jiang@volvocars.com");
        //se.doSendHtmlEmail("沃尔沃销售每日快讯", mail_content, "lc.chen@volvocars.com");
        se.doSendHtmlEmail("沃尔沃销售每日快讯", mail_content, "dongyi.sheng@volvocars.com");  
        se.doSendHtmlEmail("沃尔沃销售每日快讯dongyi.sheng", mail_content, "1003441170@qq.com");
       
        
        
    }
}
